package software;

import java.util.Random;

abstract class BinaryOperation {

	static final int UPPER = 100;
	static final int LOWER = 0;
	private int left_operand=0;
	private int right_operand=0;
	private int value=0;
	private char operator='+';
	
	// 不是构造方法
	abstract int calculate(int left,int right) ;
	abstract boolean checkingCalculation(int anInteger);
	
	//用于构造混合算式
	public void SetBinaryOperation(int left,int right,char anOperator){
		left_operand = left;
		right_operand = right;
		operator = anOperator;
		value = calculate(left,right);
	}
	//生成一个混合习题
	protected void generateBinaryOperation(char anOperator) {
		int left,right,result;
	    Random random = new Random();
		left = random.nextInt(UPPER+1);
		do {
			right = random.nextInt(UPPER+1);
			result = calculate(left,right);
		}while(!(checkingCalculation(result)));
		left_operand = left;
		right_operand = right;
		operator = anOperator;
		value = result;
	 }
	//重载等于函数
	public boolean equals (BinaryOperation anOperation) { 
		return 	left_operand == anOperation.getLeftOperand() &
				right_operand == anOperation.getRightOperand() &
				operator == anOperation.getOperator();			
	}
	
	// 访问实例变量的访问器，共有方法
	public int getLeftOperand(){return left_operand;}
	public int getRightOperand(){return right_operand;}
	public char getOperator(){return operator;}
	public int getResult(){return value;}

	// 返回字符串“32+5”
	public String toString(){

		return ""+left_operand+getOperator()+right_operand;
	}
	//返回字符串“32+5=”
	public String asString(){
		return toString()+"=";
	}
	//返回字符串“32+5=37”
	public String fullString(){
		return toString()+"="+getResult();
	}
}	
